﻿/* -----------------------------------------------------------------------
 * <copyright file="OPScoreQuery.cs" company="O-RID">
 *     Copyright (c)2010 O-RID Co.,Ltd.
 * </copyright>
 * <author>Li Dawei Ariles.Zhang</author>
 * <summary> 
 *          OP 总成绩 查询
 *          作成日：06/13/2010
 *          
 *          08/19/2010 修改 前台OPID框。
 *                          当选择的是 所有OP或组时。用TAG值作为判断条件，用text值作为显示值
 *                          选择部分OP时，方法不变
 * </summary>
 * -----------------------------------------------------------------------
 */

namespace Orid.Management.Statistics.OperatorUnitMangement.Command.OP
{
    using System;
    using System.Windows;
    using System.Windows.Media;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.FunctionList;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.SearchCtrl;
    using Orid.Management.Statistics.OperatorUnitMangement.WCFServers.OPQueryRef;
    using childWindow = Orid.Management.Statistics.OperatorUnitMangement.Common.Window;
    using Wcf = Orid.Management.Statistics.OperatorUnitMangement.Common.Service;

    /// <summary>
    /// OP总成绩查询命令 实现接口ICommand
    /// </summary>
    public class OPScoreQuery : ICommand
    {
        #region 变量定义

        /// <summary>
        /// 查询区域对象 SelAnyContainer
        /// </summary>
        private SelAnyContainer container;

        /// <summary>
        /// 错误消息 ErrorMessageBox
        /// </summary>
        private childWindow.MessageBox errorMessbox;
 
        /// <summary>
        /// 查询服务定义 wcfOPScoreClient
        /// </summary>
        private WCFServers.OPQueryRef.WCFOPCorrelationClient wcfOPScoreClient;
               
        /// <summary>
        /// 日志服务定义 wcfLogClient
        /// </summary>
        private WCFLog4Ref.WCFLog4Client wcfLogClient;

        #endregion  变量定义

        /// <summary>
        /// 构造函数 OPScoreQuery
        /// </summary>
        /// <param name="container">查询控件对象 Container</param>
        public OPScoreQuery(SelAnyContainer container)
        {
            this.container = container;

            this.InitField();
        }

        /// <summary>
        /// 查询结束事件 ExecOver
        /// </summary>
        public event EventHandler<SelectOPScoreCompletedEventArgs> ExecOver;

        /// <summary>
        /// 查询开始事件 ExecBegin
        /// </summary>
        public event EventHandler<EventArgs> ExecBegin;

        #region ICommand 成员

        /// <summary>
        /// 命令执行方法 Exec
        /// </summary>
        public void Exec()
        {
            // this.tbOPID.Text dpDateFrom dpDateTo内容必选的判断
            // 日期不能相同
            // 开始日期不能小于结束日期
            int error = FunctionClass.IsSelectDo(this.container.dpDateFrom, this.container.dpDateTo, this.errorMessbox);

            if (error == 0)
            {
                this.container.btnSelect.IsEnabled = true;
                return;
            }
            
            // 把WHERE值组合好后放入到CWP中，再由CWP传向clientWhere
            WCFServers.OPQueryRef.WhereParameters cwp = new WhereParameters();

            try
            {
                try
                {
                    #region 把参数加到ObservableCollection中
                    // 日期的判断条件
                    // 日期返回空值用"null"
                    if (!String.IsNullOrEmpty(this.container.dpDateFrom.Text))
                    {
                        cwp.WhereentryDateFrom = this.container.dpDateFrom.SelectedDate.Value.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        cwp.WhereentryDateFrom = "null";
                    }

                    if (!String.IsNullOrEmpty(this.container.dpDateTo.Text))
                    {
                        cwp.WhereentryDateTo = this.container.dpDateTo.SelectedDate.Value.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        cwp.WhereentryDateTo = "null";
                    }                                 

                    #endregion 把参数加到ObservableCollection中
                }
                catch (Exception ex)
                {
                    this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + " OP总成绩查询接口实现类 OPScoreQuery 转换查询条件出错" + ex.Message.ToString());

                    Common.FunctionList.FunctionClass.ShowMessageBox(
                       this.errorMessbox,
                       1,
                       "转换查询条件出错",
                       ex.Message.ToString(),
                       Colors.Gray,
                       true,
                       Visibility.Visible,
                       Visibility.Collapsed);
                    return;
                }

                  
                this.OnExecBegin(null, null);

                this.wcfOPScoreClient.SelectOPScoreAsync(cwp);
            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + "OP总成绩查询接口实现类 OPScoreQuery 调用OPEntryScoreQueryInfor服务出错" + ex.Message.ToString());

                Common.FunctionList.FunctionClass.ShowMessageBox(
                   this.errorMessbox,
                   1,
                   "调用OPEntryScoreQueryInfor服务出错",
                   ex.Message.ToString(),
                   Colors.Gray,
                   true,
                   Visibility.Visible,
                   Visibility.Collapsed);
                return;
            }
        }

        #endregion

        /// <summary>
        /// 查询结束事件 OnExecOver
        /// </summary>
        /// <param name="sender">Sender Object</param>
        /// <param name="e">e type of SelectOPScoreCompletedEventArgs</param>
        protected void OnExecover(object sender, SelectOPScoreCompletedEventArgs e)
        {
            if (this.ExecOver != null)
            {
                this.ExecOver(this, e);
            }
        }

        /// <summary>
        /// 查询开始事件 OnExecBegin
        /// </summary>
        /// <param name="sender">Sender of Obejct</param>
        /// <param name="e">e type of EnentArgs</param>
        protected void OnExecBegin(object sender, EventArgs e)
        {
            if (this.ExecBegin != null)
            {
                this.ExecBegin(this, e);
            }
        }

        /// <summary>
        /// 初始化变量 InitField
        /// </summary>  
        private void InitField()
        {
            this.wcfOPScoreClient = Wcf.Service.OPScoreQuery;
            this.wcfLogClient = Wcf.Service.Log4;
            this.errorMessbox = new childWindow.MessageBox();
            this.wcfOPScoreClient.SelectOPScoreCompleted += 
                new EventHandler<SelectOPScoreCompletedEventArgs>(this.OnExecover);

            this.container.cbWorkEfficiency.Visibility = Visibility.Collapsed;
            this.container.lbEffciency.Visibility = Visibility.Collapsed;
            this.container.tbOPID.Visibility = Visibility.Collapsed;
            this.container.OPID.Visibility = Visibility.Collapsed;
            this.container.btnOPID.Visibility = Visibility.Collapsed;

            if (this.container.btnOPID.Visibility == Visibility.Collapsed) 
            {
                Thickness thick = new Thickness();
                thick.Left = 535;
                thick.Right = 0;
                thick.Top = 0;
                thick.Bottom = 0;

                this.container.btnSelect.Margin = thick;
            }
        } 
    }
}
